package com.kangyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.kangyi.app.mapper.GuestsStayingMapper;
import com.kangyi.app.mapper.UserMapper;
import com.kangyi.app.service.GuestService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.AddGuestDTO;
import com.kangyi.dto.CheckinApplyDTO;
import com.kangyi.entity.GuestsStaying;
import com.kangyi.entity.User;
import com.kangyi.exception.AccountException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.vo.GuestListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class GuestServiceImpl implements GuestService {

    @Autowired
    private GuestsStayingMapper guestsStayingMapper;


    @Override
    public void checkinApply(CheckinApplyDTO checkinApplyDTO) {

        GuestsStaying guestsStaying = guestsStayingMapper.selectById(checkinApplyDTO.getGuestId());
        if (guestsStaying.getNursingHomeId() != null) {
            throw new DataNotExistException(MessageConstant.PLEASE_EXIT_THE_NURSING_HOME_BEFORE_ATTEMPTING_TO_APPLY);
        }
        //修改入住人
        GuestsStaying build = GuestsStaying.builder()
                .id(checkinApplyDTO.getGuestId())
                .checkInTime(checkinApplyDTO.getCheckInTime())
                .roomFeeId(checkinApplyDTO.getRoomFeeId())
                .foodFeeId(checkinApplyDTO.getFoodFeeId())
                .specialFeeId(checkinApplyDTO.getSpecialFeeId())
                .nursingHomeId(checkinApplyDTO.getNursingHomeId())
                .urgentName(checkinApplyDTO.getUrgentName())
                .urgentPhone(checkinApplyDTO.getUrgentPhone())
                .allergy(checkinApplyDTO.getAllergy())
                .status(GuestsStaying.UNDER_REVIEW).build();
        guestsStayingMapper.update(build);
    }

    @Override
    public void addGuest(AddGuestDTO addGuestDTO) {
        GuestsStaying guestsStaying = guestsStayingMapper.getGuestByIdCard(addGuestDTO.getIdCard());

        if (guestsStaying != null) {
            throw new AccountException(MessageConstant.GUEST_EXIST);
        }
        GuestsStaying build = GuestsStaying.builder()
                .image(addGuestDTO.getHeadshot())
                .name(addGuestDTO.getName())
                .idCard(addGuestDTO.getIdCard())
                .gender(addGuestDTO.getGender())
                .birthday(addGuestDTO.getBirthday())
                .phone(addGuestDTO.getPhone())
                .relation(addGuestDTO.getRelation())
                .status(GuestsStaying.NO_SHOW)
                .userId(addGuestDTO.getUserId())
                .age(calculateAge(addGuestDTO.getBirthday()))
                .build();
        guestsStayingMapper.add(build);
    }

    @Override
    public List<GuestListVO> list(Integer userId) {
        List<GuestListVO> guestListVOList = guestsStayingMapper.list(userId);
        if (CollUtil.isEmpty(guestListVOList)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return guestListVOList;
    }

    public String calculateAge(LocalDate birthDate) {
        try {
            LocalDate currentDate = LocalDate.now();
            // 计算年龄并将结果转为 String 类型
            return String.valueOf(Period.between(birthDate, currentDate).getYears());
        } catch (Exception e) {
            System.out.println("日期格式不正确，请使用 yyyy-MM-dd 格式。");
            return "50";
        }
    }
}
